package com.example.fms.service.impl;

import ch.qos.logback.classic.spi.EventArgUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fms.dto.req.OrdersReqDto;
import com.example.fms.dto.resp.GoodsRespDto;
import com.example.fms.dto.resp.GoodsRespOneDto;
import com.example.fms.dto.resp.OrdersRespDto;
import com.example.fms.dto.resp.OrdersRespOneDto;
import com.example.fms.entity.Goods;
import com.example.fms.entity.Orders;
import com.example.fms.entity.OrdersGoods;
import com.example.fms.mapper.GoodsMapper;
import com.example.fms.mapper.OrdersGoodsMapper;
import com.example.fms.mapper.OrdersMapper;
import com.example.fms.service.GoodsService;
import com.example.fms.service.OrdersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Resource
    OrdersMapper ordersMapper;
    @Resource
    OrdersGoodsMapper ordersGoodsMapper;
    @Resource
    GoodsMapper goodsMapper;
    @Override
    public Page<OrdersRespOneDto> getOrdersPage(String query, OrdersReqDto req) {
        Page<OrdersRespDto> page = new Page<>(req.getPageNum(),req.getPageSize());
        List<OrdersRespDto> ordersPage = ordersMapper.getOrdersPage(page, req);
        Page<OrdersRespOneDto> pageOne = new Page<>(req.getPageNum(),req.getPageSize());
        List<OrdersRespOneDto> ordersRespOneDtoList = new ArrayList<>();
        if(!CollectionUtil.isEmpty(ordersPage)){
            ordersPage.forEach(s->{
                OrdersRespOneDto ordersRespOneDto = new OrdersRespOneDto();
                ordersRespOneDto.setOrder_fapiao_company(s.getOrderFapiaoCompany());
                ordersRespOneDto.setOrder_fapiao_content(s.getOrderFapiaoContent());
                ordersRespOneDto.setOrder_fapiao_title(s.getOrderFapiaoTitle());
                ordersRespOneDto.setOrder_id(s.getOrderId());
                ordersRespOneDto.setOrder_number(s.getOrderNumber());
                ordersRespOneDto.setOrder_price(s.getOrderPrice());
                ordersRespOneDto.setTrade_no(s.getTradeNo());
                ordersRespOneDto.setConsignee_addr(s.getConsigneeAddr());
                ordersRespOneDto.setIs_send(s.getIsSend());
                ordersRespOneDto.setTrade_no(s.getTradeNo());
                ordersRespOneDto.setOrder_pay(s.getOrderPay());
                ordersRespOneDto.setPay_status(s.getPayStatus());
                ordersRespOneDtoList.add(ordersRespOneDto);
            });
        }
        pageOne.setRecords(ordersRespOneDtoList);
        pageOne.setTotal(page.getTotal());
        return pageOne;
    }

    @Override
    public Boolean putOrdersById(OrdersReqDto req, Long id) {
        Orders orders = new Orders();
        orders.setOrderId(req.getOrder_id());
        orders.setIsSend(req.getIs_send());
        orders.setOrderPay(req.getOrder_pay());
        orders.setPayStatus(req.getPay_status());
        orders.setOrderPrice(req.getOrder_price());
        orders.setOrderNumber(req.getOrder_number());
        ordersMapper.updateById(orders);
        return true;
    }

    @Override
    public OrdersRespOneDto getOrdersById(Long id) {
        Orders orders = ordersMapper.selectById(id);
        OrdersRespOneDto ordersRespOneDto = new OrdersRespOneDto();
        ordersRespOneDto.setOrder_fapiao_company(orders.getOrderFapiaoCompany());
        ordersRespOneDto.setOrder_fapiao_content(orders.getOrderFapiaoContent());
        ordersRespOneDto.setOrder_fapiao_title(orders.getOrderFapiaoTitle());
        ordersRespOneDto.setOrder_id(orders.getOrderId());
        ordersRespOneDto.setOrder_number(orders.getOrderNumber());
        ordersRespOneDto.setOrder_price(orders.getOrderPrice());
        ordersRespOneDto.setTrade_no(orders.getTradeNo());
        ordersRespOneDto.setConsignee_addr(orders.getConsigneeAddr());
        ordersRespOneDto.setIs_send(orders.getIsSend());
        ordersRespOneDto.setTrade_no(orders.getTradeNo());
        LambdaQueryWrapper<OrdersGoods> qw = new LambdaQueryWrapper<>();
        qw.eq(OrdersGoods::getOrderId,orders.getOrderId());
        List<OrdersGoods> ordersGoods = ordersGoodsMapper.selectList(qw);
        List<GoodsRespOneDto> goodsRespOneDtoList = new ArrayList<>();
        List<Long> goodsList = ordersGoods.stream().map(OrdersGoods::getGoodsId).toList();
        LambdaQueryWrapper<Goods> qwg = new LambdaQueryWrapper<>();
        qwg.in(Goods::getGoodsId,goodsList);
        List<Goods> goods = goodsMapper.selectList(qwg);
        if(!CollectionUtil.isEmpty(goods)){
            goods.forEach(s-> {
                GoodsRespOneDto goodsRespOneDto = new GoodsRespOneDto();
                goodsRespOneDto.setGoods_id(s.getGoodsId());
                goodsRespOneDto.setGoods_name(s.getGoodsName());
                goodsRespOneDto.setGoods_price(s.getGoodsPrice());
                goodsRespOneDto.setGoods_number(s.getGoodsNumber());
                goodsRespOneDto.setGoods_weight(s.getGoodsWeight());
                goodsRespOneDto.setGoods_state(s.getGoodsState());
                goodsRespOneDto.setAdd_time(s.getAddTime());
                goodsRespOneDto.setHot_number(s.getHotNumber());
                goodsRespOneDto.setIs_promote(s.getIsPromote());
                goodsRespOneDtoList.add(goodsRespOneDto);
            });
        }
        ordersRespOneDto.setGoods(goodsRespOneDtoList);
        return ordersRespOneDto;
    }

    @Override
    public Boolean putAddressById(Long id, Integer consigneeAddr) {
        Orders orders = ordersMapper.selectById(id);
        orders.setConsigneeAddr(consigneeAddr.toString());
        ordersMapper.updateById(orders);
        return true;
    }
}
